home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / FREOPEN.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  2KB  |  80 lines

  1. /* This is file FREOPEN.C */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. /*
  8.  * Copyright (c) 1980 Regents of the University of California.
  9.  * All rights reserved.  The Berkeley software License Agreement
  10.  * specifies the terms and conditions for redistribution.
  11.  */
  12.  
  13. #if defined(LIBC_SCCS) && !defined(lint)
  14. static char sccsid[] = "@(#)freopen.c    5.2 (Berkeley) 3/9/86";
  15. #endif LIBC_SCCS and not lint
  16.  
  17. #include <sys/types.h>
  18. #include <sys/file.h>
  19. #include <stdio.h>
  20.  
  21. FILE *
  22. freopen(file, mode, iop)
  23.     const char *file;
  24.     register const char *mode;
  25.     register FILE *iop;
  26. {
  27.     register f, rw, oflags;
  28.     char tbchar;
  29.  
  30.     rw = (mode[1] == '+');
  31.  
  32.     fclose(iop);
  33.  
  34.     switch (*mode) {
  35.     case 'a':
  36.         oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
  37.         break;
  38.     case 'r':
  39.         oflags = rw ? O_RDWR : O_RDONLY;
  40.         break;
  41.     case 'w':
  42.         oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
  43.         break;
  44.     default:
  45.         return (NULL);
  46.     }
  47.     if (mode[1] == '+')
  48.         tbchar = mode[2];
  49.     else
  50.         tbchar = mode[1];
  51.     if (tbchar == 't')
  52.         oflags |= O_TEXT;
  53.     else if (tbchar == 'b')
  54.         oflags |= O_BINARY;
  55.     else
  56.         oflags |= (_fmode & (O_TEXT|O_BINARY));
  57.  
  58.     f = open(file, oflags, 0666);
  59.     if (f < 0)
  60.         return (NULL);
  61.  
  62.     if (*mode == 'a')
  63.         lseek(f, (off_t)0, L_XTND);
  64.  
  65.     iop->_cnt = 0;
  66.     iop->_file = f;
  67.     iop->_bufsiz = 0;
  68.     if (rw)
  69.         iop->_flag = _IORW;
  70.     else if (*mode == 'r')
  71.         iop->_flag = _IOREAD;
  72.     else
  73.         iop->_flag = _IOWRT;
  74.     if (oflags & O_TEXT)
  75.         iop->_flag |= _IOTEXT;
  76.  
  77.     iop->_base = iop->_ptr = NULL;
  78.     return (iop);
  79. }
  80.